package com.siwei.eqi.util;

import com.alibaba.fastjson2.JSON;

import java.util.*;

public class RegionUtil {
    /**
     * 通过省份code获取区县CODE
     *
     * @param code 省份 codes
     * @return 区县code
     * @throws Exception http请求异常
     */
    public static StringBuilder getRegion(String code) throws Exception {
        String[] split = code.split(",");
        List<String> codeList = Arrays.asList(split);
        StringBuilder codesBuilder = new StringBuilder();
        List<String> list = Arrays.asList("820000", "810000", "710000");
        for (String data : codeList) {
            if (list.contains(data)) {
                codesBuilder.append(",");
                codesBuilder.append(data);
            }
        }
        String url = "http://219.153.49.10:7810/sj_assets/v6/api/ecomn/region_boundary?op=select";
        Map<String, String> params = new HashMap<>();
        if (code.equals("100000")) {
            params.put("parent_id", code);
        } else {
            params.put("province_code", code);
            params.put("region_level", "3");
        }
        params.put("ak", "mf1ce8ac07b3ce12dc25a41c3ea4c26339");

        // 请求省级数据
        String s = HttpUtil.get(url, params, new HashMap<>());
        Map<String, Object> parse = (Map<String, Object>) JSON.parse(s);
        Map<String, Object> result = (Map<String, Object>) parse.get("result");
        Object itemList1 = result.get("item_list");
        if (itemList1 instanceof Collection) {
            List<Map<String, String>> itemList = (List<Map<String, String>>) result.get("item_list");
            for (Map<String, String> stringStringMap : itemList) {
                codesBuilder.append(",");
                codesBuilder.append(stringStringMap.get("code"));
            }
            codesBuilder.deleteCharAt(0);
        }

        return codesBuilder;
    }

    /**
     * 查询区县占地面积
     *
     * @param code
     * @return
     * @throws Exception
     */
    public static List<Map<String, String>> getRegionBoundary(StringBuilder code) throws Exception {
        List<Map<String, String>> regionBoundary = new ArrayList<>();
        String url = "http://219.153.49.10:7810/sj_assets/v6/api/ecomn/lucc_stats";
        Map<String, String> params = new HashMap<>();
        params.put("op", "select");
        params.put("region_code", code.toString());
        params.put("ak", "mf1ce8ac07b3ce12dc25a41c3ea4c26339");
        String s = HttpUtil.get(url, params, new HashMap<>());
        Map<String, Object> parse = (Map<String, Object>) JSON.parse(s);
        Map<String, Object> result = (Map<String, Object>) parse.get("result");
        Object itemObject = result.get("item_list");
        if (itemObject instanceof Collection) {
            regionBoundary = (List<Map<String, String>>) result.get("item_list");
        }

        return regionBoundary;
    }

    /**
     * 通过code省市
     *
     * @param code 省份 codes
     * @return 区县code
     * @throws Exception http请求异常
     */
    public static List<String> getCity(String code) throws Exception {
        List<String> cityList = new ArrayList<>();
        String url = "http://219.153.49.10:7810/sj_assets/v6/api/ecomn/region_boundary?op=select";
        Map<String, String> params = new HashMap<>();
        if (code.equals("100000")) {
            params.put("parent_id", code);
        } else {
            params.put("province_code", code);
            params.put("region_level", "3");
        }
        params.put("ak", "mf1ce8ac07b3ce12dc25a41c3ea4c26339");

        // 请求省级数据
        String s = HttpUtil.get(url, params, new HashMap<>());
        Map<String, Object> parse = (Map<String, Object>) JSON.parse(s);
        Map<String, Object> result = (Map<String, Object>) parse.get("result");
        Object itemList1 = result.get("item_list");
        if (itemList1 instanceof Collection) {
            List<Map<String, String>> itemList = (List<Map<String, String>>) result.get("item_list");
            for (Map<String, String> stringStringMap : itemList) {
                cityList.add(stringStringMap.get("name"));
            }
        }
        return cityList;
    }
}
